package com.ccl.keyassistant.utils;


/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.utils
 *文件名     FileUtil
 *创建者     ccl
 *创建时间   2019-04-06 13:58
 *注解       文件工具类
 */


import com.ccl.keyassistant.entity.LocalFile;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;


public class FileUtil {

    /****
     * 计算文件大小
     *
     * @param length
     * @return
     */
    public static String getFileSzie(Long length) {
        if (length >= 1048576) {
            return (length / 1048576) + "MB";
        } else if (length >= 1024) {
            return (length / 1024) + "KB";
        } else if (length < 1024) {
            return length + "B";
        } else {
            return "0KB";
        }
    }


    /**
     * 字符串时间戳转时间格式
     *
     * @param timeStamp
     * @return
     */
    public static String getStrTime(String timeStamp) {
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        long l = Long.valueOf(timeStamp) * 1000;
        timeString = sdf.format(new Date(l));
        return timeString;
    }


    public static String getTime(){
        long l = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(l);
        return format;
    }


    public static ArrayList<LocalFile> getFileList(File root){
        if(root!=null && root.isDirectory()){
            File[] files = root.listFiles();
            if(files==null){
                return null;
            }
            ArrayList<LocalFile> mLocalFile = new ArrayList<>();

            LocalFile lf = null;
            for (File file : files) {
                //格式化文件大小
                String fileSzie = FileUtil.getFileSzie(file.length());

                lf = new LocalFile(file.getName(), file.getAbsolutePath(), fileSzie, file.isDirectory());

                mLocalFile.add(lf);
            }
            Collections.sort(mLocalFile,new FileNameComparator());
            LocalFile ret = new LocalFile("...", root.getParent(), "0", true);
            mLocalFile.add(0,ret);
            return mLocalFile;
        }
        return null;
    }


    /**
     * 根据文件名进行比较排序
     */
    public static class FileNameComparator implements Comparator<LocalFile> {
        protected final static int
                FIRST = -1,
                SECOND = 1;

        @Override
        public int compare(LocalFile lhs, LocalFile rhs) {
            if (lhs.isDirectory() || rhs.isDirectory()) {
                if (lhs.isDirectory() == rhs.isDirectory()) {
                    return lhs.getFileName().compareToIgnoreCase(rhs.getFileName());
                }
                else if (lhs.isDirectory()) {
                    return FIRST;
                }
                else {
                    return SECOND;
                }
            }
            return lhs.getFileName().compareToIgnoreCase(rhs.getFileName());
        }
    }


}
